package com.zhugang.week01;

/**
 * @program algorithms
 * @description: 零矩阵
 * @author: chanzhugang
 * @create: 2022/03/28 22:27
 */
public class MatrixZero {


    /**
     * 把哪一行哪一列要设置为0用Boolea[]记录下来
     *
     * @param matrix
     */
    public void setZero(int[][] matrix) {
        int n = matrix.length;
        if (n == 0) {
            return;
        }
        int m = matrix[0].length;
        boolean[] zeroRows = new boolean[n];
        boolean[] zeroClos = new boolean[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 0) {
                    zeroRows[i] = true;
                    zeroClos[j] = true;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (zeroRows[i] || zeroClos[j]) {
                    matrix[i][j] = 0;
                }
            }
        }

    }
}